package com.kyrie.lib_sys.zTest.netflow;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * controller层
 *
 * @author heefM
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/flow")
@Slf4j
public class FlowController {

    @Autowired
    private FlowService flowService;

    /**
     * 导入excel文件数据
     * @param file
     * @return
     */
    @PostMapping("/import/excel")
    public ResponseEntity<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 参数判空
            if (file == null || file.isEmpty()) {
                return ResponseEntity.badRequest().body("文件不能为空，请选择要上传的CSV文件");
            }
            
            int importedCount = flowService.importFromExcel(file);
            int totalCount = flowService.getRecordCount();
            return ResponseEntity.ok("成功导入 " + importedCount + " 条记录，当前总共 " + totalCount + " 条记录");
        } catch (Exception e) {
            log.error("导入文件失败", e);
            return ResponseEntity.badRequest().body("导入失败: " + e.getMessage());
        }
    }


}